package com.ljy.testStream;

import org.junit.Test;

import java.io.*;

public class Stream {
    @Test
    public void testFileOutputStream() throws Exception {
        File file = new File("C:\\Users\\AikeTech\\Desktop\\WebCrawler\\1.txt");
        if (file.exists()) System.out.println("存在");
        else {
            System.out.println("不存在");
            file.createNewFile();
        }
        FileOutputStream fileOutputStream=null;
        try {
            fileOutputStream = new FileOutputStream(file);
            String str="1\n" +
                    " 中国近年来的伟大成就\n" +
                    " https://wenku.baidu.com/view/6b8fd686d838376baf1ffc4ffe4733687f21fc68.html?fr=search-4-X-income1&fixfr=CLg2NrgAi077JqQbHadagw%3D%3D\n" +
                    " 2\n" +
                    " 19大报告解读之五年十大成就\n" +
                    " https://wenku.baidu.com/view/99aef56e6d85ec3a87c24028915f804d2a168752.html?fr=search-4-X-income2&fixfr=QSz9DR0aQHN6pER%2Fksl0sg%3D%3D\n" +
                    " 3\n" +
                    " 主要成就\n" +
                    " https://wenku.baidu.com/view/abdf321ec0c708a1284ac850ad02de80d5d806d3.html?fr=search-4-X-income3&fixfr=EEQgAs28R6kH60xHbygh0w%3D%3D\n";
            fileOutputStream.write(str.getBytes());
            fileOutputStream.flush();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            fileOutputStream.close();
        }
    }
    @Test
    public void testOutputStream() throws FileNotFoundException {
        File file = new File("C:\\Users\\AikeTech\\Desktop\\WebCrawler\\成就.txt");
        if (file.exists()) System.out.println("存在");
        else System.out.println("不存在");
    }
    @Test
    public void testFileInputStream(){
        File file = new File("C:\\Users\\AikeTech\\Desktop\\WebCrawler\\1.txt");
        FileInputStream fileInputStream=null;
        try {
            fileInputStream = new FileInputStream(file);
            byte[] bytes=new byte[1024];
            int len=0;
            StringBuilder stringBuilder=new StringBuilder();
            while ((len=fileInputStream.read(bytes))!=-1) {
                String s = new String(bytes, 0, len);
                stringBuilder.append(s);
            }
            System.out.println(stringBuilder.toString());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
